package com.ruoyi.hov.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.dynamic.DynamicQueryServiceImpl;
import com.ruoyi.hov.ClassBindHovUtils;
import com.ruoyi.hov.entity.ClassBindHov;
import com.ruoyi.hov.mapper.ClassBindHovMapper;
import com.ruoyi.hov.service.ClassBindHovService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 属性绑定hov 服务实现类
 * </p>
 *
 * @author XiaShiLong
 * @since 2021-04-01
 */
@Service
public class ClassBindHovServiceImpl extends DynamicQueryServiceImpl<ClassBindHovMapper, ClassBindHov> implements ClassBindHovService {

    /**
     * 是否有绑定hovId
     *
     * @param hovId hovId
     * @return boolean
     */
    @Override
    public boolean existByHovId(Serializable hovId) {
        return this.count(Wrappers.<ClassBindHov>lambdaQuery().eq(ClassBindHov::getHovId, hovId)) > 0;
    }

    /**
     * 保存
     *
     * @param classBindHov <see>ClassBindHov</see>
     * @return boolean
     */
    @Override
    public boolean saveAll(Collection<ClassBindHov> classBindHov) {
        boolean b = this.saveBatch(classBindHov);
        if (b) {
            classBindHov.forEach(ClassBindHovUtils::setHovCache);
        }
        return b;
    }


    /**
     * 删除
     *
     * @param classBindHov <see>ClassBindHov</see>
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(ClassBindHov classBindHov) {
        boolean b = this.removeById(classBindHov.getId());
        if (b) {
            ClassBindHovUtils.clearHovCache(classBindHov);
        }
        return b;
    }

    /**
     * 根据类和字段获取hov绑定信息
     *
     * @param classPath     类路径
     * @param attributeName 字段名
     * @return ClassBindHov
     */
    @Override
    public ClassBindHov get(String classPath, String attributeName) {
        return this.getOne(
                Wrappers.<ClassBindHov>lambdaQuery()
                        .eq(ClassBindHov::getClassPath, classPath)
                        .eq(ClassBindHov::getAttributeName, attributeName)
        );
    }

    /**
     * 获取类下所有hov绑定信息
     *
     * @param classPath 类路径
     * @return List
     */
    @Override
    public List<ClassBindHov> get(String classPath) {
        return this.list(
                Wrappers.<ClassBindHov>lambdaQuery()
                        .eq(ClassBindHov::getClassPath, classPath)
        );
    }
}
